package com.weiaibenpao.demo.chislim.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.api.Url;
import com.weiaibenpao.demo.chislim.base.BaseFragment;
import com.weiaibenpao.demo.chislim.db.preferences.LocationPreferences;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.ui.activity.discover.MallActivity;
import com.weiaibenpao.demo.chislim.ui.activity.discover.MatchActivity;
import com.weiaibenpao.demo.chislim.ui.activity.discover.TourismActivity;
import com.weiaibenpao.demo.chislim.ui.activity.discover.WebViewActivity;
import com.weiaibenpao.demo.chislim.utils.LoadingDialogUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/4.
 */

public class DiscoverFragment extends BaseFragment {


    @BindView(R.id.matchLayout)
    RelativeLayout matchLayout;
    @BindView(R.id.mallLayout)
    RelativeLayout mallLayout;
    @BindView(R.id.tourismLayout)
    RelativeLayout tourismLayout;
    @BindView(R.id.gymLayout)
    RelativeLayout gymLayout;

    Intent intent;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    private String latitude,longitude;
    private LoadingDialogUtils.Builder dialog;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_discover;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        intent = new Intent();
        dialog = new LoadingDialogUtils.Builder(getContext()).setTitle("正在加载...");
    }


    @OnClick({R.id.matchLayout, R.id.mallLayout, R.id.tourismLayout, R.id.gymLayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.matchLayout:
                //前往官方赛事
                startActivity(new Intent(getActivity(), MatchActivity.class));
                break;
            case R.id.mallLayout:
                //前往商城
                startActivity(new Intent(getActivity(), MallActivity.class));
                break;
            case R.id.tourismLayout:
                //前往旅游推荐
                startActivity(new Intent(getActivity(), TourismActivity.class));
                break;
            case R.id.gymLayout:
                Toast.makeText(getContext(),"暂未开放，敬请期待",Toast.LENGTH_SHORT).show();
//                dialog.show();
//                //前往健身房
//               initMap();
                //http://www.djq42195.com/weixinh5/#/index.html?coordinate=30.189,120.1457&userId=404&userName=null&userHeader=https%3A%2F%2Fup-fd.zol-img.com.cn%2Fg5%2FM00%2F0C%2F0F%2FChMkJ1pK3viIVq2AAAAeGQNP6BcAAjuoQMk8p8AAB4x776.jpg
//                Intent intent = new Intent(getActivity(), WebViewActivity.class);
//                intent.putExtra("h5_url",urlStr);
//                intent.putExtra("h5_title","智能健身");
//                startActivity(intent);
                break;
        }
    }
    //初始化定位信息
    private void initMap() {
        mLocationClient = new AMapLocationClient(getContext());
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置返回地址信息，默认为true


        //设置定位监听
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        //定位成功回调信息，设置相关消息
                        latitude = amapLocation.getLatitude()+"";
                        longitude = amapLocation.getLongitude()+"";

                    } else {
                        //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                        latitude= LocationPreferences.getInstance().getLocationLatitude();
                        longitude= LocationPreferences.getInstance().getLocationLongitude();
                    }
                }
                String urlStr = Url.BASE_URL+ "weixinh5/#/index.html?coordinate=" + latitude+"," +longitude
                        + "&userId=" + UserPreferences.getInstance().getInt(UserPreferences.USER_ID) +
                        "&userName=" + UserPreferences.getInstance().getString(UserPreferences.USER_NAME) + "&userHeader=" +
                        UserPreferences.getInstance().getString(UserPreferences.USER_AVATAR);
                WebViewActivity.startSelf(getContext(),urlStr,"智能健身",0);
            }
        });
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        //设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //设置是否只定位一次,默认为false

        mLocationClient.startLocation();
    }

    @Override
    public void onPause() {
        super.onPause();
        dialog.dismiss();
    }
}
